package eop.graham

class DataController {
	DataService loadDataFromTsvFile

	def index = { render (view:"importdata") }

	def siteHome = { redirect (url:"/eop.graham") }

	def importdata = {

		if (params.researcher in Experiment.RESEARCH_TEAM) {

			def startTime = System.currentTimeMillis()
			def failureList = []

			DataService x = new DataService()
			// check file exists before trying to load data
			def dFile = new File("${params.dataFile}")
			if (dFile.exists() && dFile.canRead()){
				try {
					failureList = x.loadDataFromTsvFile(params.dataFile)
				}catch(Exception e)
				{
					return [status:"Error processing file: "+e.message]
				}
				def elapsedTime = (System.currentTimeMillis()-startTime)/1000
				def errorCount = failureList.size()
				String errorString = (failureList.size > 0)?"${errorCount} errors at input file line(s) ${failureList}":"no errors detected"
				return [status: "Data added in ${elapsedTime} seconds, with ${errorString}"]
			}
			else {
				return [status:"Data not added: file ${params.dataFile} cannot be read. Check name!"]
			}
		}
		else {
			return [status:"Data not added: \"${params.researcher}\" not in researcher list ${Experiment.RESEARCH_TEAM}"]
		}
	}

	def scaffold = true

}
